package com.xilihui.xlh.core.util;

import android.os.Handler;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xilihui.xlh.R;
import com.xilihui.xlh.core.app.BaseApplication;


public class ToastUtil {

    private static int duration = 1500;

    public static void toastShortPositive(String msg) {

        if (TextUtils.isEmpty(msg)) {
            return;
        }
        final Toast toast = getToast(msg, R.mipmap.ic_toast_positive);
        toast.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
    }

    public static void toastShortPositive(@StringRes int strRes) {

        if (TextUtils.isEmpty(BaseApplication.get().getString(strRes))) {
            return;
        }
        final Toast toast = getToast(BaseApplication.get().getString(strRes), R.mipmap.ic_toast_positive);
        toast.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
    }

    public static void toastLongPositive(String msg) {

        if (TextUtils.isEmpty(msg)) {
            return;
        }
        Toast toast = getToast(msg, R.mipmap.ic_toast_positive);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

    public static void toastLongPositive(@StringRes int strRes) {

        if (TextUtils.isEmpty(BaseApplication.get().getString(strRes))) {
            return;
        }
        Toast toast = getToast(BaseApplication.get().getString(strRes), R.mipmap.ic_toast_positive);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

    public static void toastShortNegative(String msg) {

        if (TextUtils.isEmpty(msg)) {
            return;
        }
        final Toast toast = getToast(msg, R.mipmap.ic_toast_negative);
        toast.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
    }

    public static void toastShortNegative(@StringRes int strRes) {

        if (TextUtils.isEmpty(BaseApplication.get().getString(strRes))) {
            return;
        }
        final Toast toast = getToast(BaseApplication.get().getString(strRes), R.mipmap.ic_toast_negative);
        toast.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
    }

    public static void toastLongNegative(String msg) {

        if (TextUtils.isEmpty(msg)) {
            return;
        }
        Toast toast = getToast(msg, R.mipmap.ic_toast_negative);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

    public static void toastLongNegative(@StringRes int strRes) {

        if (TextUtils.isEmpty(BaseApplication.get().getString(strRes))) {
            return;
        }
        Toast toast = getToast(BaseApplication.get().getString(strRes), R.mipmap.ic_toast_negative);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.show();
    }

    private static Toast getToast(String msg, @DrawableRes int drawRes) {

        Toast toast = new Toast(BaseApplication.get());

        View view = LayoutInflater.from(BaseApplication.get()).inflate(R.layout.toast, null);
        ImageView iv = (ImageView) view.findViewById(R.id.iv);
        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText(msg);
        iv.setImageResource(drawRes);
        toast.setGravity(Gravity.CENTER, 0, 0);

        toast.setView(view);
        return toast;
    }

    public static void toastException() {
        Toast toast = new Toast(BaseApplication.get());
        toast.setGravity(Gravity.CENTER, 0, 0);

        toast.setView(LayoutInflater.from(BaseApplication.get()).inflate(R.layout.toast_exception, null));
        toast.show();
    }

    public static void toast(@StringRes int strRes) {
        final Toast toast = new Toast(BaseApplication.get());
        View view = LayoutInflater.from(BaseApplication.get()).inflate(R.layout.toast_text_bottom, null);
        ((TextView) view.findViewById(R.id.tv)).setText(strRes);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, getDp(70));

        toast.setView(view);
        toast.show();

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
    }

    
    public static void toast(String str) {
        final Toast toast = new Toast(BaseApplication.get());
        View view = LayoutInflater.from(BaseApplication.get()).inflate(R.layout.toast_text_bottom, null);
        ((TextView) view.findViewById(R.id.tv)).setText(str);
        toast.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM, 0, getDp(70));
        toast.setView(view);

        toast.show();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                toast.cancel();
            }
        }, duration);
    }

    private static int getDp(int i) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i, BaseApplication.get().getResources().getDisplayMetrics());
    }
}
